<!DOCTYPE html>
<html>
	<head th:include="/common/head :: head">
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
	</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="add_form">
			<div class="layui-form-item">
				<label for="userName" class="layui-form-label">计划人姓名</label>
				<div class="layui-input-inline">
					<select  id="userName" name="userName" lay-search=""></select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="managerName" class="layui-form-label">审核人姓名</label>
				<div class="layui-input-inline">
					<select  id="managerName" name="managerName" lay-search=""></select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="month" class="layui-form-label">计划月份</label>
				<div class="layui-input-inline">
					<input type="text" id="month" name="month" required=""
						lay-verify="month" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item layui-form-text">
              	<label for="content" class="layui-form-label"><span
					class="x-red">*</span>计划内容
				</label>
              <div class="layui-input-block">
                  <textarea  id="content" name="content" autocomplete="off" class="layui-textarea"></textarea>
              </div>
          </div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" lay-filter="add_form" lay-submit="">添加</button>
			</div>
		</form>
	</div>
</body>
<script>
	var addFormData={}
	$(function(){
		//获取用户姓名、id下拉表
		getUserId();
		
		initAddForm();
	})
	//初始化添加表单
	function initAddForm(){
		layui.use(['form','layer'],function(){
			$ = layui.jquery;
			var form = layui.form,
			layer = layui.layer;
			//验证规则
			form.verify({
				content:function(value){
					if(value.length <10){
						return '计划内容最少得10个字符';
					}
				}
			});
			//监听提交
			form.on('submit(add_form)',function(data){
				console.log(data);
				//console.log(data.field);
				addFormData = data.field;
				addPlan()
				return false;
			});
		});
	}
	//添加计划
	function addPlan(){
			$.ajax({
				url:"/plan/doAddPlan",
				type:"post",
				data:{
					"userName":addFormData.userName,
					"managerName":addFormData.managerName,
					"month":addFormData.month,
					"content":addFormData.content
				},
				success:function(result){
					console.log(JSON.stringify(result));
					if(result.status==0){
						layer.msg(result.msg,function(){
							//关闭弹出层
							var index = parent.layer.getFrameIndex(window.name);
						    parent.layer.close(index);
						    //刷新当前页面
							//parent.location.replace(parent.location.href);
	                    });
					}else{
						layer.msg(result.msg);
					}
				}
			})
	}
	//年月选择器
	layui.use('laydate', function(){
	    var laydate = layui.laydate;
		laydate.render({
		    elem: '#month'
		    ,type: 'month'
		  });
	});
	//异步改为同步  async:false;
	//获取用户姓名、id列表
	function getUserId(){
		$.ajax({
			url:"/user/getUserIds",
			type:"post",
			async:false,
			success:function(result){
				if(result.status==0){
					showUserName(result);	
				}else{
					layer.msg(result.msg);
				}
			}	
		})
	}
	//展示下拉框
	function showUserName(result){
		var str= "";
		var list = result.data;
		for(i in list){
			str+="<option  value="+list[i].id+">"+list[i].userName+"</option>";
		}
		$("#userName").append(str);
		$("#managerName").append(str);
	}

</script>
</html>